package com.cliff.joinus;

import java.io.File;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.cliff.joinus.dataobject.ImageItem;
import com.cliff.joinus.dataobject.MusicItem;
import com.cliff.joinus.dataobject.VideoItem;

public class LocalMediaDataManager {

	protected static LocalMediaDataManager instance = new LocalMediaDataManager();
	
	public static LocalMediaDataManager getInstance() {
		return instance;
	}

	protected Context context = null;

	protected ArrayList<ImageItem> imageList = null;	
	protected ArrayList<MusicItem> musicList = null;
	protected ArrayList<VideoItem> videoList = null;
		
	protected final String SELECTION_MUSIC = MediaStore.Audio.Media.IS_MUSIC+"!=0";	
	protected final String SORTORDER_MUSIC = MediaStore.Audio.Media.ALBUM+","+MediaStore.Audio.Media.TRACK;
	
	public static enum MEDIA_TYPE {IMAGE, AUDIO, VIDEO};
	public static enum FILE_TYPE {LOCAL_FILE, EXTERNAL_URL};	
	
	protected LocalMediaDataManager() {	
	}
	
	public ArrayList<ImageItem> getImageList() {
		return imageList;
	}
	
	public ArrayList<MusicItem> getMusicList() {
		return musicList;
	}

	public ArrayList<VideoItem> getVideoList() {
		return videoList;
	}

	protected ArrayList<ImageItem> obtainImageItemList() {
		String[] projection = new String[] { MediaStore.Images.Media.DATA };
		ContentResolver resolver = context.getContentResolver();
		Cursor c = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				projection, null, null, null);
		ArrayList<ImageItem> retList = new ArrayList<ImageItem>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {			
			ImageItem item = new ImageItem();
			item.filePathName = c.getString(0);
			retList.add(item);
		}
		c.close();
		return retList;
	}
	
	protected ArrayList<MusicItem> obtainMusicItemList() {
		String[] projection = new String[] { 
				MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.TITLE,		
				MediaStore.Audio.Media.DISPLAY_NAME,
				MediaStore.Audio.Media.ALBUM, 
				MediaStore.Audio.Media.ARTIST, 
				MediaStore.Audio.Media.DURATION 
		};
		
		ContentResolver resolver = context.getContentResolver();
		Cursor c = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				projection, SELECTION_MUSIC, null, SORTORDER_MUSIC);
		ArrayList<MusicItem> retList = new ArrayList<MusicItem>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {			
			MusicItem item = new MusicItem();
			item.filePathName = c.getString(0);
			item.title = c.getString(1);
			item.displayName = c.getString(2);
			item.album = c.getString(3);
			item.artist = c.getString(4);
			item.duration= c.getLong(5);
			retList.add(item);
		}
		c.close();
		return retList;
	}

	protected ArrayList<VideoItem> obtainVideoItemList() {
		String[] projection = new String[] { 
				MediaStore.Video.Media.DATA,
				MediaStore.Video.Media.RESOLUTION,
				MediaStore.Video.Media.DURATION,
				MediaStore.Video.Media.TITLE,
				MediaStore.Video.Media.DISPLAY_NAME,
				MediaStore.Video.Media.ARTIST
		};
		
		ContentResolver resolver = context.getContentResolver();
		Cursor c = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				projection, null, null, null);
		ArrayList<VideoItem> retList = new ArrayList<VideoItem>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {			
			VideoItem item = new VideoItem();
			item.filePathName = c.getString(0);
			item.resolution = c.getString(1);
			item.duration = c.getLong(2);
			item.title = c.getString(3);
			item.displayName = c.getString(4);
			item.artist = c.getString(5);
			retList.add(item);
		}
		c.close();
		return retList;
	}
	
	public void setContext(Context context) {
		this.context = context;
	}
	
	public boolean updateLocalMediaList() {
		if (null == context)
			return false;
		
		if (null != imageList)
			imageList.clear();
		if (null != musicList)
			musicList.clear();
		if (null != videoList)
			videoList.clear();

		imageList = obtainImageItemList();
		musicList = obtainMusicItemList();
		videoList = obtainVideoItemList();
		
		return true;
	}
		
	public void playExternal(String filePath, MEDIA_TYPE mt, FILE_TYPE ft) {
		if (null == context)
			return;
		
		String type = null;
		Uri uri = null;
		
		switch (mt) {
		case IMAGE:
			type = "image/*"; break;
		case AUDIO:
			type = "audio/*"; break;
		case VIDEO:
			type = "video/*"; break;			
		}
		
		if (FILE_TYPE.LOCAL_FILE == ft)
			uri = Uri.fromFile(new File(filePath));
		else
			uri = Uri.parse(filePath);
		
		Intent intent = new Intent();  
		intent.setAction(android.content.Intent.ACTION_VIEW);			
		intent.setDataAndType(uri, type);  
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
		context.startActivity(intent);					
	}

}
